package com.supermarket.view;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swtchart.Chart;
import org.eclipse.swtchart.IAxis;
import org.eclipse.swtchart.IAxisSet;
import org.eclipse.swtchart.IAxisTick;
import org.eclipse.swtchart.IBarSeries;
import org.eclipse.swtchart.IGrid;
import org.eclipse.swtchart.ILegend;
import org.eclipse.swtchart.ILineSeries;
import org.eclipse.swtchart.LineStyle;
import org.eclipse.swtchart.ISeries.SeriesType;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ibm.icu.text.SimpleDateFormat;
import com.supermarket.Dao.ShopDao;
import com.supermarket.model.Shop;
import com.supermarket.util.Dbutil;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Spinner;

public class Shopchart extends Composite {
	private ShopDao shopdao=new ShopDao();
	private Dbutil dbutil=new Dbutil();
	private Table table;
	private Combo combo;
	private TableItem tableItem;
	private Combo combo_2;
	private Label lblAsasas ;
	private Label lblAsasas_3;
	private Label label_3_1;
	private Spinner spinner;
//	private double a1=0;
//	private double b1=0;
//	private double c1=0;
//	private String a;
//	private String b;
//	private String c;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public Shopchart(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(243, 95, 95));
		setForeground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		
		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 23, SWT.NORMAL));
		lblNewLabel.setForeground(SWTResourceManager.getColor(255, 250, 250));
		lblNewLabel.setBackground(SWTResourceManager.getColor(243, 95, 95));
		lblNewLabel.setBounds(30, 23, 281, 65);
		lblNewLabel.setText("商品流量统计");
		
		Label label = new Label(this, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 43, SWT.BOLD));
		label.setBackground(SWTResourceManager.getColor(243, 95, 95));
		label.setForeground(SWTResourceManager.getColor(255, 250, 250));
		label.setBounds(30, 84, 53, 58);
		label.setText("\u201C");
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_BACKGROUND));
		
		composite.setBounds(43, 148, 1013, 549);
		
		Label label_1 = new Label(this, SWT.NONE);
		label_1.setText("\u201C");
		label_1.setForeground(SWTResourceManager.getColor(255, 250, 250));
		label_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 43, SWT.BOLD));
		label_1.setBackground(SWTResourceManager.getColor(243, 95, 95));
		label_1.setBounds(1073, 692, 53, 50);
		table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int i=table.getSelectionIndex();
				lblAsasas_3.setText(i+1+" ");
			}
		});
		
		
		table.setBounds(62, 89, 895, 387);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tableColumn_4 = new TableColumn(table, SWT.NONE);
		tableColumn_4.setWidth(100);
		tableColumn_4.setText("年份");
		
		TableColumn tableColumn_5 = new TableColumn(table, SWT.NONE);
		tableColumn_5.setWidth(133);
		tableColumn_5.setText("季度");
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(122);
		tableColumn.setText("商品种类");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(152);
		tableColumn_1.setText("商品名称");
		
		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(123);
		tableColumn_2.setText("销售数量");
		
		TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
		tableColumn_3.setWidth(260);
		tableColumn_3.setText("营收额");
		
		combo = new Combo(composite, SWT.READ_ONLY);
		combo.setItems(new String[] {"  "});
		combo.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		combo.setBounds(654, 35, 203, 33);
		
		Label label_2 = new Label(composite, SWT.NONE);
		label_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		label_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_2.setBounds(525, 32, 113, 33);
		label_2.setText("商品种类：");
		
//		fillchart();
//		String[] char1 = {a,b,c};
//		double[] char2 = {a1,b1,c1};
//		
//		
//		Chart chart = new Chart(composite, SWT.NONE);
//		chart.setBounds(0, 76, 444, 281);
//		chart.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
//		

//		
//		
//		
//		//设置X,Y轴
//		IAxisSet axisSet = chart.getAxisSet();
//		
//		//刻度颜色
//		IAxisTick xTick = axisSet.getXAxis(0).getTick();
//		IAxisTick yTick = axisSet.getYAxis(0).getTick();
//		Color black = new Color(Display.getDefault(), 0, 0, 0);
//		xTick.setForeground(black);
//		yTick.setForeground(black);
//		
//		
//		//设置标题，X,Y轴名称颜色
//		chart.getTitle().setText("最受欢迎TOP3的商品");
//		chart.getAxisSet().getXAxis(0).getTitle().setText("商品名称");
//		chart.getAxisSet().getYAxis(0).getTitle().setText("商品销售数量");
//		
//		IAxis xAxis = axisSet.getXAxis(0);
//		xAxis.setCategorySeries(char1);
//		xAxis.enableCategory(true);
//		
//		chart.getTitle().setForeground(black);
//		chart.getAxisSet().getXAxis(0).getTitle().setForeground(black);
//		chart.getAxisSet().getYAxis(0).getTitle().setForeground(black);
//		
//		//网格样式
//		IGrid xGrid = axisSet.getXAxis(0).getGrid();
//		IGrid yGrid = axisSet.getYAxis(0).getGrid();
//		xGrid.setStyle(LineStyle.NONE);
//		yGrid.setStyle(LineStyle.SOLID);
//		
//		// create bar series
//
//		IBarSeries barSeries = (IBarSeries) chart.getSeriesSet()
//		    .createSeries(SeriesType.BAR, "销售数量");
//		barSeries.setYSeries(char2);		
//				
//		// adjust the axis range
//		Color color = new Color(Display.getDefault(), 80, 240, 180);
//		barSeries.setBarColor(color);
//
//		//图例
//		ILegend legend = chart.getLegend();
//		legend.setPosition(SWT.TOP);
//		
//
//		// adjust the axis range
//		chart.getAxisSet().adjustRange();
		Label label_2_1 = new Label(composite, SWT.NONE);
		label_2_1.setText("季度：");
		label_2_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		label_2_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_2_1.setBounds(84, 32, 65, 33);
		
		combo_2 = new Combo(composite, SWT.READ_ONLY);
		combo_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		combo_2.setItems(new String[] {"第一季度", "第二季度", "第三季度", "第四季度", "  "});
		combo_2.setBounds(347, 35, 143, 33);
		
		Button button = new Button(composite, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				QueryShop(e);
			}
		});
		button.setBounds(871, 35, 86, 35);
		button.setText("查询");
		
		lblAsasas = new Label(composite, SWT.NONE);
		lblAsasas.setToolTipText("");
		lblAsasas.setForeground(SWTResourceManager.getColor(0, 0, 0));
		lblAsasas.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblAsasas.setAlignment(SWT.CENTER);
		lblAsasas.setBounds(530, 503, 68, 25);
		
		Label lblAsasas_1 = new Label(composite, SWT.NONE);
		lblAsasas_1.setToolTipText("");
		lblAsasas_1.setText("\u5171\u67E5\u8BE2\u5230 :");
		lblAsasas_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblAsasas_1.setAlignment(SWT.RIGHT);
		lblAsasas_1.setBounds(285, 503, 165, 25);
		
		Label lblAsasas_2 = new Label(composite, SWT.NONE);
		lblAsasas_2.setToolTipText("");
		lblAsasas_2.setText("\u8BB0\u5F55");
		lblAsasas_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblAsasas_2.setBounds(604, 503, 60, 25);
		
		lblAsasas_3 = new Label(composite, SWT.NONE);
		lblAsasas_3.setToolTipText("");
		lblAsasas_3.setForeground(SWTResourceManager.getColor(255, 102, 0));
		lblAsasas_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblAsasas_3.setAlignment(SWT.RIGHT);
		lblAsasas_3.setBounds(456, 503, 68, 25);
		
		Label label_3 = new Label(composite, SWT.NONE);
		label_3.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		label_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_3.setBounds(23, 497, 143, 36);
		label_3.setText("季度营业额：");
		
		label_3_1 = new Label(composite, SWT.NONE);
		label_3_1.setForeground(SWTResourceManager.getColor(255, 102, 0));
		label_3_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		label_3_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_3_1.setBounds(172, 497, 143, 36);
		
		spinner = new Spinner(composite, SWT.BORDER | SWT.READ_ONLY);
		spinner.setMaximum(2050);
		spinner.setSelection(2020);
		spinner.setBounds(155, 34, 123, 34);
		
		filltable(null,null,null);
		fillcombo();
		
		//System.out.println(a1+" "+b1+" "+c1);
		//System.out.println(a+" "+b+" "+c);
		
		
	}

	private void QueryShop(SelectionEvent e) {
		lblAsasas_3.setText("0");
		int index0=combo.getSelectionIndex();
		int index2=combo_2.getSelectionIndex();
		String SType=null;
		String year=spinner.getText();
		String quarter=null;
		if(index0!=-1) {
			 SType=combo.getItem(index0);
		}
		
		if(index2!=-1) {
			 quarter=combo_2.getItem(index2);
		}	
		filltable(SType,year,quarter);
		
	}

	/**
	 * 填充类别
	 */
	private int fillcombo() {
		Connection con=null;
		int n=0;
		try {
			con=dbutil.getcon();
			ResultSet rs=shopdao.Queryshop(con);
			while(rs.next()) {
				combo.add(rs.getString("SType"));
				n++;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				dbutil.closeCon(con);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return n;
	}
//	/**
//	 * 填充统计表
//	 * @return
//	 */
//	private void fillchart() {
//		Connection con=null;
//		//double[] yAsis;
//		try {
//			con=dbutil.getcon();
//			ResultSet rs=shopdao.Shopchart(con);
//			int i=0;
//			while(rs.next()) {
//
//				if(i==0) {
//					a=rs.getString("SName");
//					a1=rs.getDouble("Num");
//				}
//				if(i==1) {
//					b=rs.getString("SName");
//					b1=rs.getDouble("Num");
//				}
//				if(i==2) {
//					c=rs.getString("SName");
//					c1=rs.getDouble("Num");
//
//				}
//				i++;
//				
//			}
//			
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally {
//			try {
//				dbutil.closeCon(con);
//			} catch (Exception e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}
//		}
//	}

	/**
	 * 初始化表格
	 * 
	 */
	private void filltable(String SType,String year,String quarter) {
		TableItem tableItems[] = table.getItems();//得到所有的tableItem
        for(int i = 0; i<tableItems.length; i++)
        {
            tableItems[i].dispose();//释放
        	
        }
		Connection con=null;
		int n=0;
		try {
			con=dbutil.getcon();
			ResultSet rs=null;
			float sum=0;
			rs=shopdao.Shoptable(con, year, quarter,SType);
			while(rs.next()) {
				n++;
				tableItem = new TableItem(table, SWT.NONE);
				if(n%2==1) {
					tableItem.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
				}else {
					tableItem.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
				}
				tableItem.setText(new String[] {rs.getString("年份"),rs.getString("季度"),rs.getString("SType"),rs.getString("SName"),Float.toString(rs.getFloat("sum1")),Float.toString(rs.getFloat("summoney"))});
			    sum+=rs.getInt("summoney");
			  
			}
			lblAsasas.setText("/ "+n);
			label_3_1.setText(Float.toString(sum));
			   
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				dbutil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
